package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.SysConfigDao;
import com.bringway.p2p.dao.TjrDao;


/**
 * 
 * @author qjy
 * @version 创建时间：2015年12月14日 上午9:34:42
 * @功能：抽奖结果页面
 */

/**
 * @author dell
 *
 */
@Repository
public class SysconfigDaoImpl  extends P2PJdbcDao implements SysConfigDao{

	Logger logger = Logger.getLogger(SysconfigDaoImpl.class);
	/**
	 * @author qjy
	 * SysConfigDao 的实现类
	 * 系统配置数据
	 */
	@Override
	public List querySysConfig(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String id = params.optString("id");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String syskey = params.optString("syskey");
		String sysvalue = params.optString("sysvalue");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		sql.append(" SELECT ID, CONFIG_KEY, CONFIG_VALUE,date_format(CREATE_DATE,'%Y-%m-%d %H:%i:%s') CREATE_DATE FROM d_ccb_fm.t_fm_sys_config");
		sql.append("   where 1=1 ");
		if(StringUtils.isNotBlank(startDate)){
			sql.append(" AND date_format(CREATE_DATE,'%Y-%m-%d %H:%i:%s') >= :startDate ");
			paramMap.put("startDate", startDate);
		}
		if(StringUtils.isNotBlank(endDate)){
			sql.append(" AND date_format(CREATE_DATE,'%Y-%m-%d %H:%i:%s') <= :endDate ");
			paramMap.put("endDate", endDate);
		}
		if(StringUtils.isNotEmpty(id)){
			sql.append(" AND t1.USER_NAME like :id");
			paramMap.put("id", "%"+id+"%");
		}
		if(StringUtils.isNotEmpty(syskey)){
			sql.append(" AND CONFIG_KEY like :syskey");
			paramMap.put("syskey", "%"+syskey+"%");
		}
		if(StringUtils.isNotEmpty(sysvalue)){
			sql.append(" AND CONFIG_VALUE like :sysvalue");
			paramMap.put("sysvalue", "%"+sysvalue+"%");
		}
		sql.append(" order by CONFIG_KEY,CREATE_DATE desc ");
		if(0 != limit){
			sql.append(" LIMIT :start,:limit");
			paramMap.put("start", start);
			paramMap.put("limit", limit);
		}
		logger.info("querySysConfig sql is " + sql.toString());
		logger.info("querySysConfig paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForList(
				sql.toString(), paramMap);

	}
	
	@Override
	public int insertSysConfig(Map<String, Object> paramMap) {
		String sql = "insert into  d_ccb_fm.t_fm_sys_config(CONFIG_KEY,CONFIG_VALUE,CREATE_DATE) values(:configkey,:configvalue,now())";
		logger.info("insertSysConfig sql is " + sql);
		logger.info("insertSysConfig paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql, paramMap);
	}
	@Override
	public int updateConfig(Map<String, Object> paramMap) {
		String sql = "update  d_ccb_fm.t_fm_sys_config set CONFIG_VALUE=:configvalue,CREATE_DATE=now() where ID=:id";
		logger.info("updateConfig sql is " + sql);
		logger.info("updateConfig paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql, paramMap);
	}
	
	@Override
	public int querySysID(String syskey) {
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		sql.append(" SELECT  count(*) FROM d_ccb_fm.t_fm_sys_config");
		sql.append("   where CONFIG_KEY=:syskey ");
		paramMap.put("syskey", syskey);
		logger.info("querySysID sql is " + sql.toString());
		logger.info("querySysID paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);

	}
	/**
	 * @author QJY
	 * 
	 */
	@Override
	public int deleteConfig(JSONObject parms) {
		StringBuffer sql = new StringBuffer();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		parmsMap.put("id", parms.opt("id"));
		sql.append("  delete from  d_ccb_fm.t_fm_sys_config where ID = :id");
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), parmsMap);
	}

}
